package com.vhall.component.framework.core.environment;

import com.vhall.component.framework.core.environment.ymld.YmldPropertySourceLoader;
import org.springframework.boot.env.PropertiesPropertySourceLoader;
import org.springframework.boot.env.PropertySourceLoader;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 搜索并加载配置文件, 当前只支持 yml,xml,properties 等配置文件，其它不作处理
 *
 * @author xiaoliang.wang@vhall.com
 */
public class PropertySourceLoaderComposite extends AbstractPropertySourceDetector {

    public static final String DEFAULT_SUFFIX = "properties";

    private final List<PropertySourceLoader> propertySourceDetectors = new ArrayList<>();

    {
        propertySourceDetectors.add(new YmldPropertySourceLoader());
        propertySourceDetectors.add(new PropertiesPropertySourceLoader());
    }

    public void addPropertySourceDetectors(List<PropertySourceLoader> sourceDetectors) {
        propertySourceDetectors.addAll(sourceDetectors);
    }

    public List<PropertySourceLoader> getPropertySourceDetectors() {
        return Collections.unmodifiableList(propertySourceDetectors);
    }

    @Override
    public String[] getFileExtensions() {
        List<String> fileExtensions = new ArrayList<>(8);
        for (PropertySourceLoader propertySourceDetector : propertySourceDetectors) {
            fileExtensions.addAll(Arrays.asList(propertySourceDetector.getFileExtensions()));
        }
        return fileExtensions.toArray(new String[0]);
    }

    /**
     * 加入 environment 环境变量
     *
     * @param environment
     * @param resource
     * @return
     * @throws IOException
     */
    public void load(ConfigurableEnvironment environment, Resource resource) throws IOException {
        List<PropertySource<?>> resources = load(resource.getFilename(), resource);
        for (PropertySource<?> propertySource : resources) {
            addPropertySource(environment, propertySource);
        }
    }

    @Override
    public List<PropertySource<?>> load(String filename, Resource resource) throws IOException {
        URL url = resource.getURL();

        if (isfile(url)) {
            String file = url.getFile();
            int index = file.lastIndexOf(".");
            String suffix;
            if (-1 == index) {
                // 如果文件没有后缀,当作properties处理
                suffix = DEFAULT_SUFFIX;
            } else {
                suffix = file.substring(index + 1);
            }
            for (PropertySourceLoader propertyLoader : propertySourceDetectors) {
                if (support(propertyLoader, suffix)) {
                    return propertyLoader.load(filename, resource);
                }
            }
        }
        return Collections.EMPTY_LIST;
    }

    /**
     * 是文件类型, 此处不作判断
     *
     * @param url
     * @return
     */
    private boolean isfile(URL url) {
        return true;
    }
}
